home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / sheriffa / sheriff.prg < prev    next >
Encoding:
Text File  |  1999-04-08  |  6.2 KB  |  275 lines

  1. #INCLUDE SlsApi.h
  2.  
  3. *----------------Sheriff Class----------------------------
  4.   
  5. DEFINE CLASS Sheriff AS CUSTOM
  6.  
  7. PROTECTED     m_hLicence,;        
  8.             m_lLastError,;        
  9.             m_cUserName,;
  10.             m_cProductID,;
  11.             m_cSecrets,;
  12.             m_bSecretsSet;
  13.  
  14. PROCEDURE Init
  15. PARAMETER cProductID, cUserName
  16.  
  17.     this.m_cProductID = cProductID
  18.     this.m_cUserName = cUserName
  19.  
  20.      * Set to non-challenge mode by defautl
  21.       * Challenge mode is set on by calling SetSecrets
  22.       this.m_bSecretsSet = .F.
  23. ENDPROC
  24.  
  25. PROCEDURE Destroy
  26. ENDPROC
  27.  
  28. PROCEDURE SetSecrets
  29. PARAMETER cSecrets
  30.  
  31.     this.m_cSecrets=cSecrets
  32.     this.m_bSecretsSet=.T.
  33. ENDPROC
  34.  
  35. FUNCTION  Succeeded()
  36.     return  this.m_lLastError = SLS_SUCCESS
  37. ENDFUNC
  38.  
  39. FUNCTION  GetLastError()
  40.     return this.m_lLastError
  41. ENDFUNC
  42.  
  43. PROCEDURE GetLastErrorMessage
  44. PARAMETER cErrorMessage
  45.  
  46.     cErrorMessage = REPLICATE(CHR(0),256)
  47.     SLS_GetErrorMessage(this.m_lLastError ,cErrorMessage)
  48. ENDPROC
  49.  
  50. FUNCTION  QueryLicenceInfo
  51. PARAMETER cLicenceInfo
  52.  
  53.     this.m_lLastError = SLS_QueryLicenceInfo(this.m_cProductID, @cLicenceInfo)
  54.     return this.Succeeded()
  55. ENDFUNC
  56.  
  57. FUNCTION  GetReference
  58. PARAMETER cReference
  59.  
  60.     this.m_lLastError = SLS_GetReference(this.m_cProductID, @cReference)
  61.     return this.Succeeded()
  62. ENDFUNC
  63.  
  64. FUNCTION  SetLicence
  65. PARAMETER cReferenceCode,cLicenceKey
  66.  
  67.     this.m_lLastError = SLS_SetLicence(this.m_cProductID, cReferenceCode,cLicenceKey)    
  68.     return this.Succeeded()
  69. ENDFUNC
  70.  
  71.  
  72. *---------
  73. *Interface
  74. *---------
  75.  
  76. FUNCTION  Register
  77. PARAMETER cProductName,cLicencePath
  78.  
  79.     this.m_lLastError = SLS_Register(this.m_cProductID,cProductName,cLicensePath)
  80.     return this.Succeeded()
  81. ENDFUNC
  82.  
  83. FUNCTION  License
  84. PARAMETER cLicence
  85.  
  86.     if(!this.m_bSecretsSet)
  87.         *Secrets Undefined
  88.         this.m_lLastError=SLS_E_BAD_SECRET
  89.         return .F.
  90.     endif
  91.  
  92.     * Create Challenge
  93.     cChallenge=REPLICATE(CHR(0),32) && size of SLS_Challenge
  94.     this.m_lLastError=SLS_CreateChallenge(this.m_cSecrets,4,cLicence,28,&cChallenge)
  95.     if(this.Succeeded())
  96.         this.m_lLastError=SLS_License(this.m_cProductID,cLicence,&cChallenge)
  97.     endif
  98.         
  99.     return this.Succeeded()
  100. ENDFUNC
  101.  
  102. FUNCTION  Request
  103. PARAMETER cRequest,cPermit
  104.  
  105.     hLicence=0
  106.     cChallenge=REPLICATE(CHR(0),32)
  107.     if(this.m_bSecretsSet)
  108.         *Create Challenge
  109.         this.m_lLastError=SLS_CreateChallenge(this.m_cSecrets,4,cRequest,4,@cChallenge)
  110.         if(this.Succeeded())
  111.             this.m_lLastError=SLS_Request(this.m_cProductID,this.m_cUserName,@cRequest,@cPermit,@hLicence,@cChallenge)
  112.             if(this.Succeeded())
  113.                 *Verify Challenge
  114.                 this.m_lLastError=SLS_VerifyChallenge(this.m_cSecrets,4,@cPermit,8,@cChallenge)
  115.             endif
  116.         endif
  117.     else
  118.         *No Challenge, set Challenge.Protocol to SLS_NO_PROTOCOL
  119.         *cChallenge.Protocol=SLS_NO_PROTOCOL
  120.         this.m_lLastError=SLS_Request(this.m_cProductID,this.m_cUserName,@cRequest,@cPermit,@hLicence,@cChallenge)
  121.     endif
  122.     
  123.     this.m_hLicence=hLicence
  124.     return this.Succeeded()
  125. ENDFUNC
  126.  
  127. FUNCTION  Updates
  128. PARAMETER cUpdate,cPermit
  129.  
  130.     cChallenge=REPLICATE(CHR(0),32)
  131.     if(this.m_bSecretsSet)
  132.         *Create Challenge
  133.         this.m_lLastError=SLS_CreateChallenge(this.m_cSecrets,4,cUpdate,8,@cChallenge)
  134.         if(this.Succeeded())
  135.             this.m_lLastError=SLS_Update(this.m_cProductID,this.m_hLicence,@cUpdate,@cPermit,@cChallenge)
  136.             if(this.Succeeded())
  137.                 *Verify Challenge
  138.                 this.m_lLastError=SLS_VerifyChallenge(this.m_cSecrets,4,@cPermit,8,@cChallenge)
  139.             endif
  140.         endif
  141.     else
  142.         *No Challenge
  143.         *cChallenge.Protocol=SLS_NO_PROTOCOL;
  144.         this.m_lLastError=SLS_Update(this.m_cProductID,this.m_hLicence,@cUpdate,@cPermit,@cChallenge)
  145.     endif
  146.  
  147.     return this.Succeeded()
  148. ENDFUNC
  149.  
  150. FUNCTION  Releases
  151. PARAMETER cRelease
  152.  
  153.     cChallenge=REPLICATE(CHR(0),32)
  154.     if(this.m_bSecretsSet)
  155.         *Create Challenge
  156.         m_lLastError=SLS_CreateChallenge(this.m_cSecrets,4,cRelease,4,@cChallenge)
  157.     else
  158.         *No Challenge
  159.         *cChallenge.Protocol=SLS_NO_PROTOCOL;
  160.         
  161.     endif
  162.  
  163.     this.m_lLastError=SLS_Release(this.m_cProductID,this.m_hLicence,@cRelease,@cChallenge)
  164.     
  165.     return this.Succeeded()
  166. ENDFUNC
  167.  
  168.  
  169. *----------
  170. *Attributes
  171. *----------
  172.  
  173. FUNCTION  IsProductInstalled
  174.  
  175.     this.m_lLastError=SLS_IsProductInstalled(this.m_cProductID)    
  176.     return this.Succeeded()
  177. ENDFUNC
  178.  
  179. FUNCTION  IsProductRegistered
  180.  
  181.     LOCAL cLicenceInfo
  182.     cLicenceInfo=SPACE(52)
  183.  
  184.     this.m_lLastError=SLS_QueryLicenceInfo(this.m_cProductID,@cLicenceInfo)    
  185.     return this.Succeeded()
  186. ENDFUNC
  187.  
  188. FUNCTION  IsLicenceDefined
  189.  
  190.     LOCAL cLicenceInfo
  191.     cLicenceInfo=SPACE(52)
  192.  
  193.     this.m_lLastError=SLS_QueryLicenceInfo(m_cProductID,@cLicenceInfo)
  194.     if(!this.Succeeded())
  195.         return .F.
  196.     endif    
  197.     
  198.     lState=FromLong(SUBSTR(cLicenceInfo,28,4))
  199.     if(lState=SLS_STATE_UNDEFINED or lState=SLS_STATE_BAD)
  200.         return .F.
  201.     endif
  202.     
  203.     return .T.
  204. ENDFUNC
  205.  
  206. FUNCTION  IsLicenceValid
  207.  
  208.     LOCAL cLicenceInfo
  209.     cLicenceInfo=SPACE(52)
  210.  
  211.     this.m_lLastError=SLS_QueryLicenceInfo(m_cProductID,@cLicenceInfo)
  212.     if(!this.Succeeded())
  213.         return .F.
  214.     endif    
  215.     
  216.     lState=FromLong(SUBSTR(cLicenceInfo,28,4))
  217.  
  218.     if(lState=SLS_STATE_OK)
  219.         return .T.
  220.     endif
  221.         
  222.     return .F.    
  223. ENDFUNC
  224.  
  225. FUNCTION  SetOptions
  226. PARAMETER cOptions
  227.               
  228.     *-
  229.     * Build the string for the SLS_MSG_DIGEST struct
  230.     *-
  231.     cMessageDigest = REPLICATE(CHR(0),16)
  232.  
  233.     *-
  234.     * Build the string for the SLS_CHALLDATA struct
  235.     *-
  236.     nSecretIndex = 0
  237.     nRandom = 0
  238.     cChallengeData = ToLong(nSecretIndex) + ;
  239.                       ToLong(nRandom) + ;
  240.                       cMessageDigest 
  241.  
  242.     *-
  243.     * Build the string for the SLS_CHALLENGE struct
  244.     *-
  245.     nProtocol = SLS_NO_PROTOCOL
  246.     if this.m_bSecretsSet
  247.           nProtocol = SLS_BASIC_PROTOCOL
  248.     endif
  249.     nSize = 4
  250.     cChallenge = ToLong(nProtocol) + ;
  251.                  ToLong(nSize) + ;
  252.                  cChallengeData
  253.  
  254.     *-
  255.     * If secrets are being used,
  256.     * create a challenge.
  257.     *-
  258.     if this.m_bSecretsSet
  259.           this.m_lLastError = SLS_CreateChallenge(this.m_aSecrets, 4, ;
  260.                                            cOption, 16, ;
  261.                                            @cChallenge)
  262.           if this.Succeeded()
  263.             this.m_lLastError = SLS_SetOptions(this.m_cProductID, this.m_hLicence, ;
  264.                                     cOptions, @cChallenge)
  265.         endif
  266.     else
  267.         this.m_lLastError = SLS_SetOptions(this.m_cProductID, this.m_hLicence, ;
  268.                                     cOptions, @cChallenge)
  269.     endif
  270.  
  271.     return this.Succeeded()
  272. ENDFUNC                  
  273.  
  274. ENDDEFINE
  275.